package com.open.capacity.user.service.impl;

import com.open.capacity.commons.Result;
import com.open.capacity.prints.util.uuid.UUIDUtils;
import com.open.capacity.user.dao.SysDictDao;
import com.open.capacity.user.dao.SysDictDataDao;
import com.open.capacity.user.model.SysDict;
import com.open.capacity.user.model.SysDictData;
import com.open.capacity.user.service.SysDictService;
import com.open.capacity.user.vo.SysDictDataVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class SysDictServiceImpl implements SysDictService {

    @Autowired
    private SysDictDao sysDictDao;


    @Autowired
    private SysDictDataDao sysDictDataDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysDict sysDict) {
        sysDict.setCreateTime(new Date());
        sysDict.setUpdateTime(sysDict.getCreateTime());

        sysDictDao.save(sysDict);
        log.info("保存字典：{}", sysDict);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(SysDict sysDict) {
        sysDict.setUpdateTime(new Date());

        sysDictDao.updateByOps(sysDict);
        log.info("修改字典：{}", sysDict);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        SysDict sysDict = sysDictDao.findById(id);

        sysDictDao.delete(id);

        log.info("删除字典：{}", sysDict);
    }


    @Override
    public SysDict findById(String id) {
        return sysDictDao.findById(id);
    }

    @Override
    public SysDict findByCode(String code) {
        return sysDictDao.findByCode(code);
    }

    @Override
    public List<SysDict> findDicts(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<SysDict> list = sysDictDao.findList(params);
        return list;
    }

    @Override
    public List<SysDict> findByParentId(String parentId) {
        return sysDictDao.findByParentId(parentId);
    }


    @Override
    public Result saveOrUpdate(SysDict sysDict, String keyValue) {
        int i = 0;

        if (StringUtils.isNotEmpty(keyValue)) {
            SysDict model = sysDictDao.findById(keyValue);
            if (model != null) {
                sysDict.setId(keyValue);
                sysDict.setUpdateTime(new Date());
                i = sysDictDao.updateByOps(sysDict);
            }
        } else {
            sysDict.setId(UUIDUtils.getOriginalUUIDWithoutBar());
            sysDict.setCreateTime(new Date());
            sysDict.setUpdateTime(sysDict.getCreateTime());
            i = sysDictDao.save(sysDict);
        }

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }

    @Override
    public Map<String, Map<String, SysDictDataVo>> getMap() {
        Map<String, Map<String, SysDictDataVo>> mapDicts = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        List<SysDict> dicts = sysDictDao.findList(params);
        Map<String, SysDictDataVo> mapDictDatas;
        for (SysDict dict : dicts) {
            mapDictDatas = new HashMap<>();
            List<SysDictData> dictDatas = sysDictDataDao.findByCode(dict.getCode());
            SysDictDataVo vo;
            for (SysDictData data : dictDatas) {
                vo = new SysDictDataVo();
                vo.setParentId(data.getParentId());
                vo.setText(data.getName());
                vo.setValue(data.getValue());
                mapDictDatas.put(data.getId(), vo);
            }
            mapDicts.put(dict.getCode(), mapDictDatas);
        }
        return mapDicts;
    }

}
